package com.partners.configure;

import com.partners.model.ValidCodeModel;
import com.partners.utils.SmsUtil;
import com.partners.utils.TimestampUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static com.partners.constant.CacheTopic.VALID;

/**
 * @author huangjunjie
 * @date 2018/7/23
 */
@Component
public class SendCode {

    @Cacheable(value = VALID)
    public ValidCodeModel sendAndGetValidCodeModel(String phone){
        ValidCodeModel validCodeModel = new ValidCodeModel();
        validCodeModel.setCode(SmsUtil.sendSms(phone));
        validCodeModel.setOverTime(TimestampUtils.timestamp(LocalDateTime.now().plusMinutes(5L)));
        return validCodeModel;
    }

    @CachePut(value = VALID)
    public ValidCodeModel cleanAndGetValidCodeModel(String phone){
        ValidCodeModel validCodeModel = new ValidCodeModel();
        validCodeModel.setCode(SmsUtil.sendSms(phone));
        validCodeModel.setOverTime(TimestampUtils.timestamp(LocalDateTime.now().plusMinutes(5L)));
        return validCodeModel;
    }

    @Cacheable(value = VALID, unless = "#result==null")
    public ValidCodeModel getValidCodeModel(String phone){
        return null;
    }
}
